<!--
 * @Author: Rachel 943701297@qq.com
 * @Date: 2022-06-29 21:11:25
 * @LastEditors: Rachel 943701297@qq.com
 * @LastEditTime: 2022-06-29 21:47:26
 * @FilePath: \JS\JSCode\全选.html
 * @Description: DOM全选练习
 * 
 * Copyright (c) 2022 by Rachel 943701297@qq.com, All Rights Reserved. 
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全选练习</title>
    <script>
        window.onload = function() {
            // checkedAllBtn
            let checkedAllBtn = document.getElementById("checkedAllBtn");
            checkedAllBtn.onclick = function(){
                let items = document.getElementsByName("items");
                for (let i = 0; i < items.length; i++) {
                    items[i].checked = true;
                }
                checkedAllBox.checked = true;
            }
            // checkedNotBtn
            let checkedNotBtn = document.getElementById("checkedNotBtn");
            checkedNotBtn.onclick = function(){
                let items = document.getElementsByName("items");
                for (let i = 0; i < items.length; i++) {
                    items[i].checked = false;
                }
                checkedAllBox.checked = false;
            }
            // checkedRevBtn
            let checkedRevBtn = document.getElementById("checkedRevBtn");
            checkedRevBtn.onclick = function(){
                let items = document.getElementsByName("items");
                checkedAllBox.checked = true;
                for (let i = 0; i < items.length; i++) {
                    items[i].checked = !items[i].checked;
                    if(!items[i].checked) {
                            checkedAllBox.checked = false;
                    }
                }
            }
            // sendBtn
            let sendBtn = document.getElementById("sendBtn");
            sendBtn.onclick = function(){
                let items = document.getElementsByName("items");
                for (let i = 0; i < items.length; i++) {
                    if(items[i].checked == true) {
                        console.log(items[i].value);
                    }
                }
                console.log("-------end--------");
            }
            // checkedAllBox
            let checkedAllBox = document.getElementById("checkedAllBox");
            checkedAllBox.onclick = function(){
                let items = document.getElementsByName("items");
                if(checkedAllBox.checked == true) {
                    for (let i = 0; i < items.length; i++) {
                        items[i].checked = true;
                    }
                }else{
                    for (let i = 0; i < items.length; i++) {
                        items[i].checked = false;
                    }
                }
            }
            // items
            let items = document.getElementsByName("items");
            for (let i = 0; i < items.length; i++) {
                items[i].onclick = function () {
                    checkedAllBox.checked = true;
                    for (let j = 0; j < items.length; j++) {
                        if(!items[j].checked) {
                            checkedAllBox.checked = false;
                            //小提升！一旦进入循环就不用判断了
                            break;
                        }
                    }
                }
            }
sendBtn
        }
    </script>
</head>
<body>
    <form action="" method="post">
        你的爱好<input type="checkbox" name="" id="checkedAllBox">全选/全不选
        <br />
        <input type="checkbox" name="items" value="足球" />足球
        <input type="checkbox" name="items" value="篮球" />篮球
        <input type="checkbox" name="items" value="羽毛球" />羽毛球
        <input type="checkbox" name="items" value="乒乓球" />乒乓球
        <br />
        <input type="button" value="全选" id="checkedAllBtn" />
        <input type="button" value="全不选" id="checkedNotBtn" />
        <input type="button" value="反选" id="checkedRevBtn" />
        <input type="button" value="提交" id="sendBtn" />
    </form>
</body>
</html>